﻿using ENCRYPT.Helper;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace ENCRYPT.sha256
{
    /// <summary>
    /// sha256摘要
    /// </summary>
    public class SHA256Encoder
    {
        /// <summary>
        /// 加密字节数组
        /// </summary>
        /// <param name="input">输入字节数组</param>
        /// <returns></returns>
        public static string encode(byte[] input)
        {
            SHA256 sha512 = new SHA256CryptoServiceProvider();
            byte[] output = sha512.ComputeHash(input);
            return StringHelper.byteToHexStr(output).ToLower();
        }
        /// <summary>
        /// 加密字符串
        /// </summary>
        /// <param name="input">输入字符串</param>
        /// <returns></returns>
        public static string encode(string input)
        {
            return encode(Encoding.UTF8.GetBytes(input));
        }

        /// <summary>
        /// 获取文件摘要
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <returns></returns>
        public static string encodeFile(string path)
        {
            SHA256 sha512 = new SHA256CryptoServiceProvider();
            FileStream inputStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
            return StringHelper.byteToHexStr(sha512.ComputeHash(inputStream)).ToLower();
        }
    }
}
